<?php
/*
 * 心情后台控制器
 * @author 索索
 */
 
 namespace Admin\Controller;
 use Think\Controller;
 
 class MoodController extends CommonController {
 	
//	列表
	public function index() {
		/**
		 * 分页操作
		 */
		 $data=array();
		 $data['status'] = array('neq',-1);
		 $page = $_REQUEST['p'] ? $_REQUEST['p']:1;
		 $pageSize = $_REQUEST['pageSize'] ?$_REQUEST['pageSize'] : 5;
		 $moods = D("Mood")->getMoods($data,$page,$pageSize);
		 $moodsCount = D("Mood")->getMoodsCount($data);
		 
		 $res = new \Think\Page($moodsCount,$pageSize);
		 $pageRes = $res->show();
//		 print_r($pageRes);
		 $this->assign('pageRes',$pageRes);
		 $this->assign('moods',$moods);
		$this->display();
	}
	
//	新增 
	public function add() {
		if($_POST){
			if(!isset($_POST['title']) || !$_POST['title']){
				return show(0, '标题不能为空');
			}
			if(!isset($_POST['txt']) || !$_POST['txt']){
				return show(0, '内容不能为空');
			}
			
			$_POST['time']= time();
			
			if($_POST['id']){
				return $this->save($_POST);
			}
			
			
			$moodId = D('Mood')->insert($_POST);
			if($moodId){
				return show(1, '添加成功！',$moodId);
			}
			return show(0, '添加失败',$moodId);
			
		}else{
			$this->display();
		}
	}
	
//修改	
	public function save($data){
		$moodId = $data['id'];
		unset($data['id']);
		try{
			$id=D("Mood")->updataMoodById($moodId,$data);
			if($id===false){
				return show(0, '更新失败');
			}
			return show(1, '更新成功');
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
	}
	
//	删除
	public function del (){
		try{
			if($_POST){
				$id=$_POST['id'];
				$status=$_POST['status'];
				$res=D('Mood')->updataStatusById($id,$status);
				if($res){
					return show(1, '操作成功！');
				}else{
					return show(0, '操作失败！');
				}
			}
		}catch(Exception $e){
			return show(0, $e->getMessage());
		}
		return show(0, '没有提交的数据');
	}
	
	
	public function edit(){
		$moodId = $_GET['id'];
		$mood=D("Mood")->find($moodId);
		$this->assign('mood',$mood);
		$this->display();
	}
	
	

	

	
	
	
		
 }
 	
